﻿package com.subway.babynote;

import com.subway.babynote.syncservice.syncapi;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
/*水*/
public class gender extends Activity implements OnClickListener {
	private static final int INVISIBLE = 0x00000004;
	static final int RG_REQUEST = 0;
	private ImageButton male_ImageButton;
	private ImageButton female_ImageButton;
	private Button login_button;
	private Button back_button;
	private Button finish_button;
	private boolean gender =false;
	private int id = -1;
	private int birth;
	public NotedbHelper notedb;
	private LinearLayout linearLayout, statistics, sync, more;
	Intent In;
	Cursor cur;
	TextView hint_view;
    ImageView male_box,female_box;
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.gender);
		notedb = new NotedbHelper(this);
		male_ImageButton = (ImageButton) findViewById(R.id.imagemale_Button);
		female_ImageButton = (ImageButton) findViewById(R.id.imagefemale_Button);
		login_button = (Button) findViewById(R.id.login_button);
		back_button = (Button) findViewById(R.id.back_button);
		finish_button = (Button) findViewById(R.id.finish_button);
		hint_view = (TextView) findViewById(R.id.hint_view);
		male_box=(ImageView) findViewById(R.id.male_checkbox);
		female_box=(ImageView) findViewById(R.id.female_checkbox);
		boolean dbgender=syncapi.getSyncImpl().getUserGender();
                gender=dbgender;
		if(dbgender){
			male_box.setBackgroundResource(R.drawable.btn_check_on);
			female_box.setBackgroundResource(R.drawable.btn_check_off);
			male_ImageButton.setBackgroundResource(R.drawable.boyafter);
            female_ImageButton.setBackgroundResource(R.drawable.girl);
		}else {
			male_box.setBackgroundResource(R.drawable.btn_check_off);
			female_box.setBackgroundResource(R.drawable.btn_check_on);
			male_ImageButton.setBackgroundResource(R.drawable.boy);
            female_ImageButton.setBackgroundResource(R.drawable.girlafter);
		}
		In = new Intent();
	//	initTabFunction();
	
		try {
			if (syncapi.getSyncImpl().getUserid() != -1) {
				login_button.setVisibility(INVISIBLE);
				login_button.setClickable(false);
			}
		} catch (Exception e) {
			Log.v("gender", "from DB e=" + e.getMessage());
		}
		Bundle bundle = this.getIntent().getExtras();
		/*
		 * birth = Integer.toString(bundle.getInt("year")) + "-" +
		 * Integer.toString(bundle.getInt("month")) + "-" +
		 * Integer.toString(bundle.getInt("day"));
		 */
		birth = bundle.getInt("birth");
		male_ImageButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {			
                male_ImageButton.setBackgroundResource(R.drawable.boyafter);
                female_ImageButton.setBackgroundResource(R.drawable.girl);
                male_box.setBackgroundResource(R.drawable.btn_check_on);
    			female_box.setBackgroundResource(R.drawable.btn_check_off);
				gender = true;
			}
		});
		female_ImageButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				female_ImageButton.setBackgroundResource(R.drawable.girlafter);
				male_ImageButton.setBackgroundResource(R.drawable.boy);
				male_box.setBackgroundResource(R.drawable.btn_check_off);
				female_box.setBackgroundResource(R.drawable.btn_check_on);
				gender = false;
			}
		});
		back_button.setOnClickListener(this);

		finish_button.setOnClickListener(this);

		login_button.setOnClickListener(this);

	}



	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		if (v.equals(back_button)) {
             this.finish();
			//In.setClass(gender.this, birth.class);
			//startActivity(In);
		} else if (v.equals(login_button)) {

			In.setClass(gender.this, login.class);
			startActivity(In);
		} else if (v.equals(finish_button)) {
			try {
				hint_view.setText("");
			/*	if (gender == -1) {
				 hint_view.setText(getResources().getString(R.string.choosegender)) ;
					return;
				}*/
		    //	cur = notedb.getUser();
			//	syncapi.getSyncImpl().informSync(
			//			syncapi.operation_type.get_userRecord().code,
			//			notedb.nowTime());
				this.startManagingCursor(cur);
				if (syncapi.getSyncImpl().getUser() != null) {
					id = syncapi.getSyncImpl().getUserid();
				/*	notedb.updateUserInfo(birth, gender, id);
					int ret = syncapi.getSyncImpl().informSync(
							syncapi.operation_type.changeUserInfo.code,
							notedb.nowTime());*/
					int ret=syncapi.getSyncImpl().updateUserInfo(birth, gender);
				} else {
					/*cur = notedb.getVisitor();
					if (cur.getCount() != 0) {
						id = notedb.getVisitorid();
					//	notedb.updateVisitorInfo(birth, gender, id);
						int ret = syncapi.getSyncImpl().informSync(
								syncapi.operation_type.changeUserInfo.code,
								notedb.nowTime());
					} else {*/
					
					//	notedb.AddVisitor(id, notedb.nowTime(), birth, gender);
						
					/*	int ret = syncapi.getSyncImpl().informSync(
								syncapi.operation_type.regVisitor.code,
								notedb.nowTime());
								
					}*/
					int ret=syncapi.getSyncImpl().AddVisitor(birth,gender);
				}
			} catch (Exception e) {
				cur.close();
				Log.v("perday", "set INfo From DB e=" + e.getMessage());

			}

			In.setClass(gender.this, maintab.class);
			startActivity(In);
			this.finish();
			this.finishActivity(RG_REQUEST);
		}

	}

	public void onDestroy() {

		notedb.close();
		super.onDestroy();
	}

}
